package com.joe.demo.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

/**
 * @Description
 * @Author gaojw
 * @Date 2024-08-30
 */
@Slf4j
public class TestRedis {

    /**
     * set key value
     * 3
     * $3
     * set
     * $4
     * name
     * $8
     * zhangsan
     * @param args
     */
    public static void main(String[] args) {
        final byte[] LINE = {13, 10};
        final NioEventLoopGroup worker = new NioEventLoopGroup();
        try {
            final Bootstrap bootstrap = new Bootstrap();
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.group(worker);
            bootstrap.handler(new ChannelInitializer<NioSocketChannel>() {
                @Override
                protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                    nioSocketChannel.pipeline().addLast(new LoggingHandler());
                    nioSocketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                        @Override
                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                            final ByteBuf buffer = ctx.alloc().buffer();
                            buffer.writeBytes("*3".getBytes(StandardCharsets.UTF_8));
                            buffer.writeBytes(LINE);
                            buffer.writeBytes("$3".getBytes(StandardCharsets.UTF_8));
                            buffer.writeBytes(LINE);
                            buffer.writeBytes("set".getBytes(StandardCharsets.UTF_8));
                            buffer.writeBytes(LINE);
                            buffer.writeBytes("$4".getBytes(StandardCharsets.UTF_8));
                            buffer.writeBytes(LINE);
                            buffer.writeBytes("name".getBytes(StandardCharsets.UTF_8));
                            buffer.writeBytes(LINE);
                            buffer.writeBytes("$8".getBytes(StandardCharsets.UTF_8));
                            buffer.writeBytes(LINE);
                            buffer.writeBytes("zhangsan".getBytes(StandardCharsets.UTF_8));
                            buffer.writeBytes(LINE);
                            ctx.writeAndFlush(buffer);
                        }

                        @Override
                        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                            final ByteBuf buf = (ByteBuf) msg;
                            log.info(buf.toString());
                        }
                    });
                }
            });
            final ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress("localhost", 6379)).sync();
            channelFuture.channel().closeFuture().sync();
        }catch (InterruptedException exception){
            exception.printStackTrace();
        }finally {
            worker.shutdownGracefully();
        }
    }
}
